package com.simba.server.components.data;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import com.simba.common.utils.StringUtil;
import com.simba.server.ServerManager;
import com.simba.server.commands.impl.fetch.FetchInfoByBarcodeCmd;
import com.simba.server.components.CommandExecutor;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.SorterParams;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;
import org.json.JSONObject;


import com.simba.common.jsonserver.JsonChannelManager;
import com.simba.common.log.LogUtil;

import com.simba.server.commands.impl.record.RecordPacketOffCmd;

public class SortInfo implements IRecordData
{
	protected static final Logger logger = Logger.getLogger(SortInfo.class);;
	private String barcode;
	private final List<String> barcodeList = new LinkedList<>();
	private String boxCode;
	private String chargeWeight;
	private int droppedSlotId;
	private int errorCodePLC;
	private int errorCodeSVR;
	private int obr;
	private String planId;
	private String plcId;
	private String roadSlot;
	private String roadSlotName;
	private int slotDistrict;
	private String slotMailType;
	private final List<Integer> slotsList = new LinkedList<>();
	private String sortingId;
	private String sorterId;
	private String sortMode;
	private int sortResultId;
	private String timePacketOff;
	private String timePacketOn;
	private Timestamp timestampPacketOff;
	private Timestamp timestampPacketOn;
	private int turnNumber;
	private int uploadFlag;
	private int uploadNum;
	private String length;
	private String width;
	private String height;
	private String volume;
	private String weight;
	private String complementType;

	private byte[] plcLdBuf;
	private long currentTime;

	public String getSorterId() {
		return sorterId;
	}

	public void setSorterId(String sorterID) {
		this.sorterId = sorterID;
	}

	public byte[] getPlcLdBuf() {
		return plcLdBuf;
	}

	public void setPlcLdBuf(byte[] plcLdBuf) {
		this.plcLdBuf = plcLdBuf;
	}

	public long getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(long currentTime) {
		this.currentTime = currentTime;
	}

	public String getComplementType() {
		return complementType;
	}

	public void setComplementType(String complementType) {
		this.complementType = complementType;
	}

	public String getBarcode()
	{
		return this.barcode;
	}

	public List<String> getBarcodeList()
	{
		return this.barcodeList;
	}

	public String getBoxCode()
	{
		return this.boxCode;
	}

	public String getChargeWeight()
	{
		return chargeWeight;
	}

	public int getDroppedSlotId()
	{
		return this.droppedSlotId;
	}

	public int getErrorCodePLC()
	{
		return this.errorCodePLC;
	}

	public int getErrorCodeSVR()
	{
		return this.errorCodeSVR;
	}

	public int getObr()
	{
		return this.obr;
	}

	public String getPlanId()
	{
		return planId;
	}

	public String getPlcId()
	{
		return this.plcId;
	}

	@Override
	public String getRecordDataName()
	{
		return "Sort-Info-Restore-Data";
	}

	public int getSlotDistrict()
	{
		return this.slotDistrict;
	}

	public String getSlotMailType()
	{
		return slotMailType;
	}

	public List<Integer> getSlotsList()
	{
		return this.slotsList;
	}

	public String getSortingId()
	{
		return this.sortingId;
	}

	public String getSortMode()
	{
		return this.sortMode;
	}

	public int getSortResultId()
	{
		return this.sortResultId;
	}

	public String getTimePacketOff()
	{
		return this.timePacketOff;
	}

	public String getTimePacketOn()
	{
		return this.timePacketOn;
	}

	public Timestamp getTimestampPacketOff()
	{
		return this.timestampPacketOff;
	}

	public Timestamp getTimestampPacketOn()
	{
		return this.timestampPacketOn;
	}

	public int getTurnNumber()
	{
		return this.turnNumber;
	}

	public int getUploadFlag()
	{
		return this.uploadFlag;
	}

	public int getUploadNum()
	{
		return this.uploadNum;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	@Override
	public void restoreData()
	{
		try
		{
			LogUtil.LogDebug(SortInfo.logger,
					"[SortInfo.restoreData] Try Restore Packet Off Again. [Barcode:" + this.barcode
							+ "]");

			CommandExecutor.executorSqlCommand(RecordPacketOffCmd.NAME, this, false);
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SortInfo.logger,
					"Restore PacketOff Message In Sort Info Error -- Exception : " + ex);
		}

	}

	public void sendPacketOffToClient()
	{
		try
		{
			JSONObject sortInfoObj = new JSONObject();

			sortInfoObj.put("ServiceName", "PacketOffResult");
			sortInfoObj.put("droppedSlot", this.getDroppedSlotId());
			sortInfoObj.put("barcode", this.getBarcode());
			sortInfoObj.put("turnNumber", this.getTurnNumber());
			sortInfoObj.put("boxCode", this.getBoxCode());
			sortInfoObj.put("slotsList", this.getSlotsList());

			String sortInfoStr = sortInfoObj.toString();
			for (Channel channel : JsonChannelManager.getInstance().getChannelList())
			{
				JsonChannelManager.getInstance().commitResponse(channel, sortInfoStr.getBytes(),
						false);
			}
		}
		catch (Exception e)
		{
		}
	}

	public void sendSortInfoToClient()
	{
		try
		{
			JSONObject sortInfoObj = new JSONObject();

			sortInfoObj.put("ServiceName", "SortInfo");
			sortInfoObj.put("Barcode", this.getBarcode());
			sortInfoObj.put("trunNumber", this.getTurnNumber());
			sortInfoObj.put("boxCode", this.getBoxCode());
			sortInfoObj.put("slotList", this.getSlotsList());
			sortInfoObj.put("obrNumber", this.obr);

			String sortInfoStr = sortInfoObj.toString();
			for (Channel channel : JsonChannelManager.getInstance().getChannelList())
			{
				JsonChannelManager.getInstance().commitResponse(channel, sortInfoStr.getBytes(),
						false);
			}
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SortInfo.logger,
					"[Method]:[sendRequestSortIntoToRemoteLogger] ,[Message]:[sort info send to remote logger error : "
							+ ex.getMessage() + "]");
		}
	}


	public void doSetUploadFlag()
	{
		if (SorterParams.getInstance().getRepeatUpload()){
			return;
		}

		/**
		 * 对于重复上件的条码， 如果是已经正常落格的， 所有信息不上传
		 */
		BarcodeInfo barcodeInfo = new BarcodeInfo();
		barcodeInfo.setBarcode(this.getBarcode());
		boolean res = CommandExecutor.executorSqlCommand(FetchInfoByBarcodeCmd.NAME, barcodeInfo,true);
		if (res)
		{
			List<SortInfo> sortInfos = barcodeInfo.getSortInfos();
			if (!sortInfos.isEmpty())
			{
				//将之前的条码记录设置为失效
				ServerManager.getInstance().getPacketExpiredTask().offerPacketExpiredToQueue(this);
				for (SortInfo info: sortInfos)
				{
					if (info.getErrorCodePLC()==0)
					{
						if(StringUtil.isNull(info.getBoxCode())||info.getBoxCode().equals("none"))
						{
							//对于所有再上件条码，无打包信息，可以上传打包信息
							this.setUploadFlag(SortCommonDefines.UPLOAD_ONLY_PACK_FLAG);
						}
						else
						{
							//对于但凡出现过一次正常落格的再上件条码，打包信息正常， 所有信息不上传
							this.setUploadFlag(SortCommonDefines.UPLOAD_NO_UPLOAD_FLAG);
							break;
						}
					}
				}
			}
		}
	}

	public void setBarcode(String mailBarcode)
	{
		this.barcode = mailBarcode;
	}

	public void setBoxCode(String boxCode)
	{
		this.boxCode = boxCode;
	}

	public void setChargeWeight(String chargeWeight)
	{
		this.chargeWeight = chargeWeight;
	}

	public void setDroppedSlotId(int droppedSlotId)
	{
		this.droppedSlotId = droppedSlotId;
	}

	public void setErrorCodePLC(int errorCodePLC)
	{
		this.errorCodePLC = errorCodePLC;
	}

	public void setErrorCodeSVR(int errorCode)
	{
		this.errorCodeSVR = errorCode;
	}

	public void setObr(int obr)
	{
		this.obr = obr;
	}

	public void setPlanId(String planId)
	{
		this.planId = planId;
	}

	public void setPlcId(String plcId)
	{
		this.plcId = plcId;
	}

	public void setSlotDistrict(int slotDistrict)
	{
		this.slotDistrict = slotDistrict;
	}

	public void setSlotMailType(String slotMailType)
	{
		this.slotMailType = slotMailType;
	}

	public void setSortingId(String sortingId)
	{
		this.sortingId = sortingId;
	}

	public void setSortMode(String sortMode)
	{
		this.sortMode = sortMode;
	}

	public void setSortResultId(int sortResultId)
	{
		this.sortResultId = sortResultId;
	}

	public void setTimePacketOff(String packetOffTime)
	{
		this.timePacketOff = packetOffTime;
	}

	public void setTimePacketOn(String timePacketOn)
	{
		this.timePacketOn = timePacketOn;
	}

	public void setTimestampPacketOff(Timestamp timestampPacketOff)
	{
		this.timestampPacketOff = timestampPacketOff;
	}

	public void setTimestampPacketOn(Timestamp timestampPacketOn)
	{
		this.timestampPacketOn = timestampPacketOn;
	}

	public void setTurnNumber(int turnNumberValue)
	{
		this.turnNumber = turnNumberValue;
	}

	public void setUploadFlag(int uploadFlag)
	{
		this.uploadFlag = uploadFlag;
	}

	public void setUploadNum(int uploadNum)
	{
		this.uploadNum = uploadNum;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getVolume() {
		return volume;
	}

	public void setVolume(String volume) {
		this.volume = volume;
	}

	public String getRoadSlot() {
		return roadSlot;
	}

	public void setRoadSlot(String roadSlot) {
		this.roadSlot = roadSlot;
	}

	public String getRoadSlotName() {
		return roadSlotName;
	}

	public void setRoadSlotName(String roadSlotName) {
		this.roadSlotName = roadSlotName;
	}
}
